package com.nervenets.web.controller.handler.admin.user;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.AuthStatus;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.user.Bank;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class BankCheckHandler extends SessionHandler<BankCheckParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(BankCheckParams params, HttpServletRequest request) throws Exception {
        final Bank bank = userDao.findById(params.getBankId(), Bank.class);
        if (null == bank) return errorMessage(MessageCode.code_404, "银行资料不存在");

        bank.setAuthStatus(params.isSuccess() ? AuthStatus.success : AuthStatus.failed);
        bank.setDetail(params.getFailedReason());
        userDao.saveOrUpdate(bank);

        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
